package cn.jlearn.proxy.agent;

import cn.jlearn.proxy.agent.container.ProxyClient;
import cn.jlearn.proxy.common.container.SwitchContainer;
import cn.jlearn.proxy.common.pojo.Constant;
import cn.jlearn.proxy.common.pojo.TunnelInfoVO;
import cn.jlearn.proxy.common.utils.NetUtil;
import cn.jlearn.proxy.common.utils.PropertiesUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;

/**
 * @author qianyingchao
 * @version 1.0
 * @date 2021/5/10 4:15 下午
 */
public class AgentStartApp {

    private final static String VERSION = PropertiesUtil.getProperty("app.version");
    private final static String HOST = PropertiesUtil.getProperty("server.host");

    private final static String PROXY_IP = PropertiesUtil.getProperty("proxy.ip");
    private final static Integer PROXY_PORT = Integer.parseInt(PropertiesUtil.getProperty("proxy.port"));

    public static String TARGET_HOST = null;
    public static Integer TARGET_PORT = null;

    public static String DOMAIN = null;
    public static String TOKEN = null;


    public static void main(String[] args) {
        String url = String.format(HOST + Constant.API_APP_VERSION_CHECK, VERSION);
        String data = NetUtil.doGet(url);
        JSONObject jsonObject = JSON.parseObject(data);
        if (!jsonObject.getString("code").equals("200")) {
            System.out.println(jsonObject.getString("message"));
            System.exit(0);
            return;
        }

        ArgsParser options = new ArgsParser().parse(args);

        TOKEN = options.getKey();
        url = String.format(HOST + Constant.API_GET_CHANNEL_INFO, TOKEN);
        data = NetUtil.doGet(url);
        jsonObject = JSON.parseObject(data);
        if (jsonObject.containsKey("data")) {
            TunnelInfoVO tunnelInfoVO = jsonObject.getObject("data", TunnelInfoVO.class);

            TARGET_HOST = tunnelInfoVO.getTargetHost();
            TARGET_PORT = tunnelInfoVO.getTargetPort();

            DOMAIN = tunnelInfoVO.getDomain();

            SwitchContainer container = new ProxyClient(PROXY_IP, PROXY_PORT);
            container.start();
        } else {
            System.out.println("无用秘钥");
            System.exit(0);
        }

    }

    public static class ArgsParser {

        @Parameter(names = {"-h", "--help"}, description = "Show this help")
        private boolean help;

        @Parameter(names = {"-v", "--version"}, description = "Program version")
        private boolean version;

        @Parameter(names = {"-k", "--key"}, description = "auth token key")
        private String key;

        public ArgsParser parse(String[] args) {

            ArgsParser options = new ArgsParser();

            JCommander jc = JCommander.newBuilder().addObject(options).build();

            try {
                jc.parse(args);
                if (options.help) {
                    jc.usage();
                    System.exit(0);
                } else if (options.version) {
                    System.out.println(VERSION);
                    System.exit(0);
                }
            } catch (ParameterException parameterException) {
                jc.usage();
                System.exit(0);
            }

            if (options.key == null) {
                System.out.println("没有秘钥");
                System.exit(0);
            }

            return options;
        }

        public boolean isHelp() {
            return help;
        }

        public boolean isVersion() {
            return version;
        }

        public String getKey() {
            return key;
        }
    }

}
